package com.ohimy.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.ohimy.entity.Admin;
import com.ohimy.entity.InfoItem;
import com.ohimy.entity.InfoSort;
import com.ohimy.entity.NewsSort;
import com.ohimy.entity.User;
import com.ohimy.service.impl.InfoService;
import com.ohimy.service.impl.NewsService;
import com.ohimy.service.impl.UserService;

@Controller("managerAction")
public class ManagerAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Resource(name = "newsService")
	private NewsService newsService;

	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "infoService")
	private InfoService infoService;

	private User user = new User();
	private Admin admin = new Admin();
	private NewsSort newsSort = new NewsSort();
	private long total;
	private List<NewsSort> newsSortList;
	private List<InfoSort> infoSortList;
	private List<InfoItem> infoItemList;

	@Override
	public String execute() throws Exception {
		if (session.get("admin") == null)
			result = "/WEB-INF/site/admin/login.jsp";
		else
			result = "/WEB-INF/site/admin/";
		return "ohimy";
	}

	public String login() throws Exception {
		if (userService.isAdmin(admin)) {
			session.put("admin", "");
			return "manager";
		} else {
			result = "/WEB-INF/site/admin/login.jsp";
			return "ohimy";
		}
	}

	/**
	 * admin显示所有的新闻栏目列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showNewsSort() throws Exception {
		newsSortList = newsService.getAllNewsSort();
		result = "/WEB-INF/site/news/news_sort.jsp";
		return "ohimy";
	}

	/**
	 * admin转到新闻页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showNews() throws Exception {
		// 得到新闻总条数
		total = newsService.getNewsCount();
		// 得到新闻类别
		newsSortList = newsService.getAllNewsSort();
		result = "/WEB-INF/site/news/news.jsp";
		return "ohimy";
	}

	/**
	 * admin显示所有的资讯栏目列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showInfoSort() throws Exception {
		infoSortList = infoService.getAllInfoSort();
		result = "/WEB-INF/site/info/info_sort.jsp";
		return "ohimy";
	}

	/**
	 * admin显示所有的资讯列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showInfo() throws Exception {
		// 得到资讯总条数
		total = infoService.getInfoItemCount();
		// 得到资讯一级目录
		infoSortList = infoService.getAllInfoSort();
		result = "/WEB-INF/site/info/info.jsp";
		return "ohimy";
	}

	/* ******************************* */

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public NewsSort getNewsSort() {
		return newsSort;
	}

	public void setNewsSort(NewsSort newsSort) {
		this.newsSort = newsSort;
	}

	public List<NewsSort> getNewsSortList() {
		return newsSortList;
	}

	public void setNewsSortList(List<NewsSort> newsSortList) {
		this.newsSortList = newsSortList;
	}

	public List<InfoSort> getInfoSortList() {
		return infoSortList;
	}

	public void setInfoSortList(List<InfoSort> infoSortList) {
		this.infoSortList = infoSortList;
	}

	public void setInfoService(InfoService infoService) {
		this.infoService = infoService;
	}

	public List<InfoItem> getInfoItemList() {
		return infoItemList;
	}

	public void setInfoItemList(List<InfoItem> infoItemList) {
		this.infoItemList = infoItemList;
	}
}